package com.xbongbong.paas.connection.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.connection.service.ConnectorService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.config.ProBaseConfig;
import com.xbongbong.pro.connector.pojo.dto.ConnectorHistoryDataInitDTO;
import com.xbongbong.pro.connector.pojo.dto.ConnectorRabbitMqDTO;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 钉钉连接器
 * 创建时间 2020/12/28 2:51 PM
 * 修改时间 2020/12/28 2:51 PM
 * @author chy
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/connection")
public class ConnectorMqController {

    private static final Logger LOG = LoggerFactory.getLogger(ConnectorMqController.class);

    @Resource
    private ConnectorService connectorService;
    @Resource
    private ProBaseConfig proBaseConfig;

    /**
     * 钉钉触发器
     * @param connectorRabbitMqDTO 关联DTO
     */
    @RequestMapping(value = "/connector/triggerSend", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String triggerSend(@RequestBody @Valid ConnectorRabbitMqDTO connectorRabbitMqDTO, BindingResult br) throws Exception{
        if (!proBaseConfig.getXbbCode().equals(connectorRabbitMqDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }
        XbbResponse<BaseVO> response = null;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                connectorService.triggerSend(connectorRabbitMqDTO);
                response = new XbbResponse<>(new BaseVO());
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 从连接器全量拉取数据
     * @param connectorHistoryDataInitDTO 关联DTO
     */
    @RequestMapping(value = "/connector/historyDataInit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String historyDataInit(@RequestBody @Valid ConnectorHistoryDataInitDTO connectorHistoryDataInitDTO, BindingResult br) throws Exception{
        if (!proBaseConfig.getXbbCode().equals(connectorHistoryDataInitDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                connectorService.pullData(connectorHistoryDataInitDTO.getCorpid());
                connectorService.pushData(connectorHistoryDataInitDTO.getCorpid(), connectorHistoryDataInitDTO.getTotalNum());
                response = new XbbResponse<>(new BaseVO());
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

}
